package com.bxs.command.service;

import com.bxs.IdsManager;
import com.sun.management.OperatingSystemMXBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

// 磁盘使用信息(要求jdk1.8以上版本)
@Service
public class SystemInfoService {
    @Autowired
    IdsManager idsManager;

    @Value(value = "${work-disk}")
    private String work_disk;

    private static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

    // 获取CPU使用率
    public static Map<String, Object> cpuLoad(){
        Map<String, Object> res = new HashMap<>();
        double cpuLoad = osmxb.getProcessCpuLoad();
        int percentCpuLoad = (int) (cpuLoad * 100);
        res.put("process_cpu_load", percentCpuLoad);
        return res;
    }

    // 获取主机信息
    public Map<String, Object> systemInfo() throws UnknownHostException {
        Map<String, Object> res = new HashMap<>();
        String version = osmxb.getVersion();                                    // 系统版本信息
        res.put("version", version);
        res.put("total_memory_size", osmxb.getTotalMemorySize() / 1024 / 1024);              // 总可用内存
        res.put("free_memory_size", osmxb.getFreeMemorySize() / 1024 / 1024);                 // 可用内存
        File file = new File(work_disk);        // 有问题需要改配置
        long totalSpace = file.getTotalSpace();
        res.put("total_size", (float)(totalSpace * 1.0 / 1024 / 1024 / 1024));    // 磁盘总量(只计算工作磁盘)
        res.put("cpu_count", Runtime.getRuntime().availableProcessors());      // 可用处理器数量
        res.put("system_name", osmxb.getName());
//        res.put("address", InetAddress.getLocalHost().getHostAddress());
        // 地址使用正在监控的网卡ip地址
        res.put("address", idsManager.monitorAddress);
        return res;
    }

    // 获取磁盘使用信息(window下可用)
    public Map<String, Object> diskInfo(){
        Map<String, Object> res = new HashMap<>();
        File file = new File(work_disk);        // 有问题需要改配置
        long totalSpace = file.getTotalSpace();
        long freeSpace = file.getFreeSpace();
        res.put("total_size", (float)(totalSpace * 1.0 / 1024 / 1024 / 1024));
        res.put("free_size", (float)(freeSpace * 1.0 / 1024 / 1024 / 1024));
        return res;
    }

    // 获取线程池工作状态信息
    public Map<String, Object> getThreadPoolInfo(){
        return idsManager.getThreadPoolStatus();
    }

    public static void main(String[] args) {
//        SystemInfoService systemInfoService = new SystemInfoService();
//        System.out.println("cpu info: ");
//        System.out.println(systemInfoService.cpuLoad());
//        System.out.println("host info");
//        System.out.println(systemInfo());
//        System.out.println("disk info");
//        System.out.println(diskInfo());
    }
}
